﻿using System;
using System.Net.Sockets;
using System.Windows.Forms;
using System.Net;
using System.Configuration;

namespace SocketChat.Client
{
    static class Program
    {
        public static string Name { get; set; }
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main() {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            while (true) {
                if ((new Login()).ShowDialog() == DialogResult.OK)
                    break;
                var result = MessageBox.Show("关闭应用程序?", "消息", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (result == DialogResult.Yes)
                    return;
            }
            try {
                var endPoint = new IPEndPoint(IPAddress.Parse(ConfigurationManager.AppSettings["ip"]), int.Parse(ConfigurationManager.AppSettings["port"]));
                Application.Run(new MainForm(endPoint, Name));
            }
            catch (OverflowException ex) {
                MessageBox.Show("端口错误!请修改配置文件!" + System.Environment.NewLine + ex.Message, "消息", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (FormatException ex) {
                MessageBox.Show("IP地址错误!请修改配置文件!" + System.Environment.NewLine + ex.Message, "消息", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (SocketException ex) {
                MessageBox.Show(System.Environment.NewLine + ex.Message, "消息", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception ex) {
                MessageBox.Show(ex.Message, "消息", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
